package team.gl.nio.svr;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;
import liuhe.LiuHe;

import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;



import team.gl.nio.cmn.Bag;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("server channelRead.. get " + ctx.channel().remoteAddress() + " msg");        
        Bag us = (Bag) msg;
        if(us.getReq().equals("currentData")){
        	System.out.println("In Walliam" + ctx.channel().remoteAddress() + " msg");
        	Bag bag = new Bag("currentData");
        	bag.setCurrentData(LiuHe.mergeManager.getMergeData());
        	ctx.channel().writeAndFlush(bag);
        }else if(us.getReq().equals("currentDataOld")) {

        	String data = LiuHe.mergeManager.getMergeDataOld();
        	Bag bag = new Bag("currentData");
        	bag.setCurrentData(data);
        	ctx.channel().writeAndFlush(bag);
        }else if(us.getReq().equals("historyData")) {
        	String issue = us.getIssue();
        	String inputTimeStr =  us.getGrabTime();
        	String data = LiuHe.mergeManager.getHistoryData(issue, inputTimeStr);
        	Bag bag = new Bag("historyData");
        	bag.setCurrentData(data);
        	ctx.channel().writeAndFlush(bag);
        }else if(us.getReq().equals("historyDataOld")) {
        	String issue = us.getIssue();
        	String inputTimeStr =  us.getGrabTime();
        	String data = LiuHe.mergeManager.getHistoryDataOld(issue, inputTimeStr);
        	Bag bag = new Bag("historyData");
        	bag.setCurrentData(data);
        	ctx.channel().writeAndFlush(bag);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();//
    }
    

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        System.out.println("server exceptionCaught.." + cause.getMessage());
        ctx.close();
    }
    

    private int counter;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    	String clientIp = ctx.channel().remoteAddress().toString().split(":")[0].substring(1); // 获取客户端 IP
        System.out.println("Incoming connection from IP: " + clientIp);

        if (!LiuHe.allowedIps.contains(clientIp)) {
            System.out.println("Connection from " + clientIp + " is not allowed, closing connection.");
            ctx.close(); // 关闭不允许连接的客户端
        } else {
            super.channelActive(ctx); // 允许连接的继续
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {       
    	String uuid = ctx.channel().id().asLongText();    	
    }



    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            // 绌洪棽6s涔嬪悗瑙﹀彂 (蹇冭烦鍖呬涪澶�)
            if (counter >= 3) {
                // 杩炵画涓㈠け3涓績璺冲寘 (鏂紑杩炴帴)
                ctx.channel().close().sync();
                System.out.println("宸蹭笌Client鏂紑杩炴帴");
            } else {
                counter++;
                System.out.println("涓㈠け浜嗙 " + counter + " 涓績璺冲寘");
            }
        }
    }

}
